package com.koron.css2.serviceManage.impl;

import com.koron.common.web.bean.DepartmentTreeBean;
import com.koron.common.web.mapper.DepartmentMapper;
import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

public class OrgNodeUpdate implements ServerInterface{
	
	private static Logger logger = LogManager.getLogger(OrgNodeUpdate.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<Void> message = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "修改成功", Void.class);
		try {
			DepartmentTreeBean bean = JsonUtils.objectToPojo(req.getData(), DepartmentTreeBean.class);
			if(StringUtils.isBlank(bean.getCode())) {
				message.setCode(Constant.MESSAGE_INT_FAIL);
				message.setDescription("参数不能为空");
				return message;
			}
			if(StringUtils.isBlank(bean.getName())) {
				message.setCode(Constant.MESSAGE_INT_FAIL);
				message.setDescription("参数不能为空");
				return message;
			}
			DepartmentMapper mapper = factory.getMapper(DepartmentMapper.class);
			int iCount = mapper.updateDepartment(bean);
			if(iCount == 0) {
				message.setCode(Constant.MESSAGE_INT_FAIL);
				message.setDescription("修改失败");
			}
		} catch (Exception e) {
			logger.error("非法参数", e);
			return MessageBean.create(Constant.ILLEGAL_PARAMETER, "非法参数", void.class);
		}
		return message;
	}
}
